package com.myyun.yun.controller;

import com.myyun.yun.domain.User;
import com.myyun.yun.service.UserService;
import com.myyun.yun.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author CC小天才
 * @website www.flyskill.cn
 * @date 2021/9/11 16:22
 **/
@RestController
@RequestMapping("/api/pri/user")
public class UserController {

    @Autowired
    private UserService userService;



    @RequestMapping("/login")
    public JsonData login(@RequestBody Map<String,Object> params){



       System.out.println("RequestBody"+params.get("phone"));

//       String row=userService.findByPhoneAndPwd(phone,password);
//
//       System.out.println("查询结果返回值： "+row);

        return JsonData.Success("");


    }

    /**
     * 注册接口
     * @param userInfo
     * @return
     */
    @PostMapping("register")
    public JsonData register(@RequestBody Map<String,String> userInfo ){


        int rows = userService.save(userInfo);

        return rows == 1 ? JsonData.Success(): JsonData.Error("注册失败，请重试");

    }


    /**
     * 根据用户id查询用户信息
     * @param request
     * @return
     */
    @GetMapping("find_by_token")
    public JsonData findUserInfoByToken(HttpServletRequest request){

        Integer userId = (Integer) request.getAttribute("user_id");

        if(userId == null){
            return JsonData.Error("查询失败");
        }

        User user =  userService.findByUserId(userId);

        return JsonData.Success(user);

    }









}
